package server;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import db.DBmanager;

/**
 * Servlet implementation class LoginServlet
 */
public class LoginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public LoginServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		PrintWriter out=response.getWriter();
		String userName="";
		String password="";
		String regRes="";
		try{
			userName=request.getParameterValues("userName")[0];
			password=request.getParameterValues("password")[0];
		}catch(Exception e){
			response.sendError(400,"illigal parameters");
			return;
		}
		
		try{
		regRes=DBmanager.getDB().loginUser(userName, password,request);
		if(regRes.equals("300"))
			response.sendError(400, "username/password does'nt match");
		else
			//response.setStatus(200);
			out.write(regRes);
		return;
		}catch(Exception e){
			e.printStackTrace();
			response.sendError(400,"we have encountred a technical problem, please login again");
			return;
		}	
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
	}

}
